﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SpringStudio.DAL.PaginationHandler
{
    /// <summary>
    /// 数据分页分析
    /// </summary>
    public static class PaginationAnalysis
    {
        /// <summary>
        /// 获取当前页的数据集
        /// </summary>
        public static DataSet GetDataSet(string connectionString, string tableName, List<string> primaryKeys, List<PaginationOrderField> orderFields,
            List<string> columns, string selectCondition, List<PaginationSqlParameter> sqlParameters, int pageSize, int pageIndex, int recordCount)
        {
            DataSet ds = new DataSet();

            switch (DataBaseTypeReader.Default)
            {
                case DataBaseType.Mssql:
                    ds = PaginationAnalysisMssql.GetDataSet(connectionString, tableName, primaryKeys, orderFields, columns,
                        selectCondition, sqlParameters, pageSize, pageIndex, recordCount);
                    break;

                case DataBaseType.Oracle:
                    ds = PaginationAnalysisOracle.GetDataSet(connectionString, tableName, primaryKeys, orderFields, columns,
                        selectCondition, sqlParameters, pageSize, pageIndex, recordCount);
                    break;
            }

            return ds;
        }

        /// <summary>
        /// 获取总记录条数
        /// </summary>
        public static int GetRecordCount(string connectionString, string tableName, string selectCondition, List<PaginationSqlParameter> sqlParameters)
        {
            int i = 0;

            switch (DataBaseTypeReader.Default)
            {
                case DataBaseType.Mssql:
                    i = PaginationAnalysisMssql.GetRecordCount(connectionString, tableName, selectCondition, sqlParameters);
                    break;

                case DataBaseType.Oracle:
                    i = PaginationAnalysisOracle.GetRecordCount(connectionString, tableName, selectCondition, sqlParameters);
                    break;
            }

            return i;
        }

        /// <summary>
        /// 得到带有编号字段的数据集
        /// </summary>
        public static DataSet GetNumberDataSet(DataSet ds, int pageSize, int pageIndex)
        {
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Columns.Contains("Pagination_Number"))
                {
                    return ds;
                }
                else
                {
                    ds.Tables[0].Columns.Add("Pagination_Number");
                    ds.Tables[0].Columns.Add("Pagination_PageIndex");
                }

                int number = (pageIndex - 1) * pageSize;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    number++;
                    dr["Pagination_Number"] = number;
                    dr["Pagination_PageIndex"] = pageIndex;
                }

                return ds;
            }
            else
            {
                throw new ArgumentException("数据集为空，或数据集中没有数据表！", "ds");
            }
        }

        /// <summary>
        /// 符合条件的所有记录集
        /// </summary>
        public static DataSet GetTotalDataSet(string connectionString, string tableName, List<string> primaryKeys, List<PaginationOrderField> orderFields,
            List<string> columns, string selectCondition, List<PaginationSqlParameter> sqlParameters)
        {
            DataSet ds = new DataSet();

            switch (DataBaseTypeReader.Default)
            {
                case DataBaseType.Mssql:
                    ds = PaginationAnalysisMssql.GetTotalDataSet(connectionString, tableName, primaryKeys, orderFields, columns,
                        selectCondition, sqlParameters);
                    break;

                case DataBaseType.Oracle:
                    ds = PaginationAnalysisOracle.GetTotalDataSet(connectionString, tableName, primaryKeys, orderFields, columns,
                        selectCondition, sqlParameters);
                    break;
            }

            return ds;
        }
    }
}
